﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Mac
 *      Date:           2013/6/30 11:33:41
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Sector7.Medical.EntityType;
using Sector7.Framework.DB;
using Sector7.Medical.Consts;

namespace Sector7.Medical.DAO.Standard
{
    /// <summary>
    /// <!-- The description about this class. -->
    /// </summary>
    public class ChargeItemDAO : DAOBase<M7_ChargeItem>,IChargeItemDAO
    {
        #region Public functions

        /// <summary>
        /// 获取收费项目
        /// </summary>
        /// <returns></returns>
        public M7_ChargeItem ReadItem(int chargItemId)
        {
            string sql = @"
                            SELECT
                            *
                            FROM M7_ChargeItem
                            WHERE 1 = 1
                            and ChargeItemId = @ChargeItemId";

            using (DBHelper helper = new DBHelper())
            {
                M7_ChargeItem chargeItem = null;

                SqlDataReader reader = helper.ExecuteQuery(sql,
                    DBUtil.CreateSqlParameter(chargItemId, "ChargeItemId"));

                while (reader.Read())
                {
                    chargeItem = new M7_ChargeItem();
                    DBUtil.ReadData(reader, chargeItem);
                }

                return chargeItem;
            }
        }

        /// <summary>
        /// 获取所有收费项目
        /// </summary>
        /// <returns></returns>
        public List<M7_ChargeItem> ReadAllItems()
        {
            string sql = @"
                            SELECT
                            *
                            FROM M7_ChargeItem c,M7_Department de
                                WHERE 1 = 1
                                AND c.DeptId = de.DeptId";

            using (DBHelper helper = new DBHelper())
            {
                List<M7_ChargeItem> chargeItemList = new List<M7_ChargeItem>();

                SqlDataReader reader = helper.ExecuteQuery(sql);
                while (reader.Read())
                {
                    var dept = new M7_Department();
                    DBUtil.ReadData(reader, dept);

                    var chargeItem = new M7_ChargeItem();
                    DBUtil.ReadData(reader, chargeItem);
                    chargeItem.Dept = dept;

                    chargeItemList.Add(chargeItem);
                }

                return chargeItemList;
            }
        }
        
        /// <summary>
        /// 添加收费项
        /// </summary>
        /// <param name="department">收费项信息</param>
        /// <returns></returns>
        public override object Create(M7_ChargeItem chargeItem)
        {
            string sql = @"
                INSERT INTO M7_ChargeItem
                        (ChargeItemName
                        ,Price
                        ,DeptId
                        ,Flags)
                    VALUES
                        (@ChargeItemName
                        ,@Price
                        ,@DeptId
                        ,@Flags)
                SELECT @@IDENTITY";

            using (DBHelper helper = new DBHelper())
            {
                return helper.ExecuteScalar(sql,
                    DBUtil.CreateSqlParameter(chargeItem.ChargeItemName, "ChargeItemName"),
                    DBUtil.CreateSqlParameter(chargeItem.Price, "Price"),
                    DBUtil.CreateSqlParameter(chargeItem.DeptId, "DeptId"),
                    DBUtil.CreateSqlParameter(chargeItem.Flags, "Flags"));
            }
        }

        /// <summary>
        /// 更新收费项
        /// </summary>
        /// <param name="employee">收费项信息</param>
        /// <returns></returns>
        public int Update(M7_ChargeItem chargeItem)
        {
            string sql = @"
                UPDATE M7_ChargeItem Set
                         ChargeItemId = @ChargeItemId
                        ,ChargeItemName = @ChargeItemName
                        ,Price = @Price
                        ,DeptId = @DeptId
                        ,Flags = @Flags";

            using (DBHelper helper = new DBHelper())
            {
                return helper.ExecuteNonQuery(sql,
                    DBUtil.CreateSqlParameter(chargeItem.ChargeItemId, "ChargeItemId"),
                    DBUtil.CreateSqlParameter(chargeItem.ChargeItemName, "ChargeItemName"),
                    DBUtil.CreateSqlParameter(chargeItem.Price, "Price"),
                    DBUtil.CreateSqlParameter(chargeItem.DeptId, "DeptId"),
                    DBUtil.CreateSqlParameter(chargeItem.Flags, "Flags"));
            }
        }
        #endregion

        #region
        public ChargeItemDAO()
        {
            m_tableName = C_M7_TableName.CHARGE_ITEM;
            m_primaryKeyName = C_ColumnName.CHARGEITEM_ID;
        }
        #endregion
    }
}
